package com.ehotting.edsta.gateway.filter;

import com.ehotting.eaf.core.utils.ip.LocalIPHelper;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

public class LogbackIpFilter   implements GlobalFilter, Ordered {

    @Autowired
    private LocalIPHelper localIPHelper;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {


        MDC.put("ipAddress", localIPHelper.getIp());
        try {
            return chain.filter(exchange);
        } finally {
            MDC.remove("ipAddress");
        }



    }

    @Override
    public int getOrder() {
        return 0;
    }
}
